package com.chenjj.java.test;

import java.math.BigInteger;
import java.util.UUID;

public class UUIDPartitionTester {
    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        int N = 100; // 切分的份数

        // 将 UUID 转换为 BigInteger，以便进行比较和计算
        BigInteger uuidValue = new BigInteger(uuid.toString().replaceAll("-", ""), 16);

        // 计算 UUID 的最小值和最大值
        BigInteger minValue = new BigInteger("00000000", 16);
        BigInteger maxValue = new BigInteger("FFFFFFFF", 16);

        // 计算每份的大小
        BigInteger range = maxValue.subtract(minValue).divide(BigInteger.valueOf(N));

        // 切分 UUID
        for (int i = 0; i < N; i++) {
            String startrowkey = minValue.add(range.multiply(BigInteger.valueOf(i))).toString(16);
            String endrowkey = minValue.add(range.multiply(BigInteger.valueOf(i + 1))).toString(16);
            System.out.println("Partition " + (i + 1) + ": [" + padWithZerosAndSuffix(startrowkey) + ", " + padWithZerosAndSuffix(endrowkey) + ")");
        }

    }

    private static String padWithZerosAndSuffix(String originalString) {
        int length = originalString.length();
        int numberOfZeros = 8 - length;
        StringBuilder paddedString = new StringBuilder();
        for (int i = 0; i < numberOfZeros; i++) {
            paddedString.append("0");
        }
        paddedString.append(originalString).append("-0000-0000-0000-000000000000");
        return paddedString.toString();
    }
}
